package com.yc.library.servlet;

import com.google.gson.Gson;
import com.yc.library.bean.Book;
import com.yc.library.dao.BookDao;
import com.yc.library.mapper.BookMapper;
import com.yc.library.util.MyBatisHelper;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Date;
import java.util.List;
import java.util.Map;

@WebServlet("/QueryBookServlet/*")
public class QueryBookServlet extends BaseServlet {

//    private BookDao bdao = new BookDao();


    protected void find(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/*        Gson gson = new Gson();
        String json = gson.toJson(bdao.query());
        response.getWriter().print(json);*/
        String name = request.getParameter("name");
        String press = request.getParameter("press");
        String date = request.getParameter("date");
        if(press=="") {
            System.out.println(name + "-----" + press);
        }
      /*  Date date=new Date();*/
//        List<Map<String,Object>> list = bdao.query(name, press, date);
        SqlSession session = MyBatisHelper.getsSession();
        BookMapper mapper = session.getMapper(BookMapper.class);
//        da==null?date = null: date = Date.valueOf(da);

        List<Book> list = mapper.selectByNameOrPressorOrDate(name, press,date);
        System.out.println(list);

        if( list == null || list.isEmpty() ) { // 查无数据
            this.send(response, 500, null);
            return;
        }
        this.send(response, 200, list);
//        session.close();
    }

    protected void finds(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        SqlSession session = MyBatisHelper.getsSession();
        BookMapper mapper = session.getMapper(BookMapper.class);

        List<Book> list = mapper.selectAll();

        if( list == null || list.isEmpty() ) { // 查无数据
            this.send(response, 500, null);
            return;
        }
        this.send(response, 200, list);
//        session.close();
    }



}
